package com.mic.yzzn.plc.common.instructions.response;

import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;

import com.mic.yzzn.plc.common.instructions.basic.PLCRequestInstruction;
import com.mic.yzzn.plc.common.instructions.basic.PLCResponseInstruction;

public class PLCDataRegisterReadResponse extends PLCResponseInstruction
{
	/** 返回的数据 */
	protected ArrayList<String> responseData;


	public void setResponseData(ArrayList<String> responseData)
	{
		this.responseData = responseData;
	}

	public PLCDataRegisterReadResponse()
	{
		new PLCResponseInstruction();
		
        instructionTypeCode = PLCResponseInstruction.PLCInstructionTypeResponseCode;
		responseCode = PLCRequestInstruction.READ_DATA_AREA_INS;
		responseData = new ArrayList<String>();
	}
	
	/**
	 * 计算每一位的异或并返回
	 */
	@Override
	protected int getXORByBit()
	{
		int i,j = 0;
		String dataString;
		int result = 0;
		
		for (i = 0; i < responseData.size(); ++i)
		{
			dataString = responseData.get(i);
			for (j = 0; j < dataString.length(); ++j)
			{
				result ^= dataString.charAt(j);
			}
		}
		
		return super.getXORByBit() ^ result;
	}
	
	/**
	 * 拼接成字符串
	 */
	@Override
	public String toString()
	{
        // 拼接指令字符串
        StringBuffer buffer = new StringBuffer();
        buffer.append(instructionHeader);
        buffer.append(locationNumber_high);
        buffer.append(locationNumber_low);
        buffer.append(instructionTypeCode);
        buffer.append(responseCode);
        
        for (int i = 0; i < responseData.size(); ++i)
        {
        	buffer.append(responseData.get(i));
        }
        
        buffer.append(getBBC_high());
        buffer.append(getBBC_low());
        buffer.append(instructionEnd);

        return buffer.toString();
	}
	
    @Override
    public String initWithString(String instructionString) throws IllegalArgumentException
    {
    	String tempResult = super.initWithString(instructionString);
    	
    	// 检测是否已经出错
    	if (tempResult.length() == 0)
		{
			return tempResult;
		}
    	
    	// 判断响应码与当前指令的响应码是否符合
    	int requestCodeLength = responseCode.length();
    	if (!(StringUtils.substring(tempResult, 0, requestCodeLength).equals(responseCode)))
		{
			throw new IllegalArgumentException("PLCSingleContactorReadInstruction's requestCode must be \"" + responseCode +"\"");
		}
    	
    	// 截取响应码
    	tempResult = StringUtils.substringAfter(tempResult, responseCode);
//    	System.out.println(tempResult);
    	    	
    	// 添加数据
    	for (int i= 0; i < tempResult.length(); i += 4)
    	{
    		String data = tempResult.substring(i, i + 4);
    		// 处理数据
    		responseData.add(getRealDataRegisterData(data));
    	}
    	
    	// 截断数据
    	tempResult = tempResult.substring(0, tempResult.length());
    	
        instructionString = toString();
    	return tempResult;
    }
    
    // Getter
    public ArrayList<String> getResponseData()
	{
		return responseData;
	}

	/**
	 * 将数据寄存器返回的数据进行正确的拼接
	 * @param orginalData
	 * @return 正确顺序的数据
     */
	public static String getRealDataRegisterData(String orginalData)
	{
		StringBuffer realData = new StringBuffer();
		realData.append(orginalData.charAt(3 - 1));
		realData.append(orginalData.charAt(4 - 1));
		realData.append(orginalData.charAt(1 - 1));
		realData.append(orginalData.charAt(2 - 1));
		return realData.toString();
	}
}
